////
////  SmartConfigVC.m
////  FishTank
////
////  Created by Scott Zhu on 14/11/24.
////  Copyright (c) 2014年 Scott Zhu. All rights reserved.
////
//
//#import "SmartConfigVC.h"
//#import <SystemConfiguration/CaptiveNetwork.h>
//#import "getgateway.h"
//#import "AQWifiConfig.h"
//#import "NetConnectInfo.h"
//#import "LANFindServer.h"
//#import "DeviceInfo.h"
//#import "Reachability.h"
//
//@interface SmartConfigVC ()
//@property (strong, nonatomic) NSString *ssid;
//@property (strong, nonatomic) NSTimer *timer;
//@property (strong, nonatomic) AQWifiConfig *aqWifiConfig;
//@property (strong, nonatomic) LANFindServer *lanFindServer;
//@property (nonatomic) NSInteger timerCount;
//
//@end
//
//@implementation SmartConfigVC
//@synthesize ssid = _ssid;
//@synthesize ssidTextField = _ssidTextField;
//@synthesize passwordTextField = _passwordTextField;
//@synthesize progressView = _progressView;
//@synthesize startConfigButton = _startConfigButton;
//@synthesize cancelConfigButton = _cancelConfigButton;
//
//@synthesize timer = _timer;
//@synthesize aqWifiConfig = _aqWifiConfig;
//@synthesize lanFindServer = _lanFindServer;
//@synthesize timerCount = _timerCount;
//
//- (void)viewDidLoad
//{
//    [super viewDidLoad];
//    NSLog(@"%@", [self fetchSSIDInfo]);
//    _ssid = [[self fetchSSIDInfo] objectForKey:@"SSID"];
//    [_ssidTextField setText:_ssid];
//    
////    _ssidTextField.layer.borderWidth = 2.0;
////    _ssidTextField.layer.borderColor = IGreen.CGColor;
////    _ssidTextField.layer.cornerRadius = 4.0;
////    
////    _passwordTextField.layer.borderWidth = 2.0;
////    _passwordTextField.layer.borderColor = IGreen.CGColor;
////    _passwordTextField.layer.cornerRadius = 4.0;
//    
////    _startConfigButton.backgroundColor = IGreen;
//    _startConfigButton.layer.cornerRadius = 4.0;
////    [_startConfigButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
////    
////    _cancelConfigButton.backgroundColor = IGreen;
//    _cancelConfigButton.layer.cornerRadius = 4.0;
////    [_cancelConfigButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    
//    _timerCount = 0;
//    
//}
//
//
//- (void)didReceiveMemoryWarning
//{
//    [super didReceiveMemoryWarning];
//    // Dispose of any resources that can be recreated.
//}
//
//#pragma mark - Actions
//- (IBAction)startConfigPressed:(id)sender
//{
//    
//    
//    if ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != ReachableViaWiFi)
//    {
//        UIAlertView *noWiFiAlert = [[UIAlertView alloc] initWithTitle:@"请先连接WiFi"
//                                                              message:nil
//                                                             delegate:self
//                                                    cancelButtonTitle:@"确定"
//                                                    otherButtonTitles:nil, nil];
//        [noWiFiAlert setTag:NO_WIFI_ALERT];
//        [noWiFiAlert show];
//        return;
//    }
//    
//    if ([[_ssidTextField text] isEqualToString:@""])
//    {
//        [[[UIAlertView alloc] initWithTitle:@"请输入SSID"
//                                   message:nil
//                                  delegate:nil
//                         cancelButtonTitle:@"确定"
//                         otherButtonTitles:nil, nil] show];
//        return;
//    }
//    
//    [_startConfigButton setHidden:YES];
//    [_cancelConfigButton setHidden:NO];
//    
//    _timer = [NSTimer scheduledTimerWithTimeInterval:1
//                                              target:self
//                                            selector:@selector(timerUpdate:)
//                                            userInfo:nil
//                                             repeats:YES];
//    [_timer fire];
//    
//    NSString *myIP = [[[NetConnectInfo alloc] init] LocalWiFiIPAddress];
//    NSString *gatewayIP = [[[NetConnectInfo alloc] init] GetConfigAddress];
//    NSLog(@"%@ %@", myIP, gatewayIP);
//    
//    [self.aqWifiConfig Start:_ssidTextField.text key: _passwordTextField.text
//                        gwip: [NSString stringWithFormat:@"%@", gatewayIP]];
//    
//    [self.lanFindServer Start];
//    __weak typeof (self) weakSelf = self;
//    [self.lanFindServer setNewDeviceCallback:^(WifiDevice *obj)
//    {
//        
//        NSLog(@"%@", [obj description]);
//        DeviceInfo *deviceInfo = [[DeviceInfo alloc] initWithDid:obj.m_did
//                                                      macAddress:obj.m_mac
//                                                        password:obj.m_password
//                                                              ip:obj.m_lanIp
//                                                          isLock:obj.m_lock];
//        DeviceListVC *deviceListVC = [[weakSelf.navigationController viewControllers] objectAtIndex:0];
//        
//        [weakSelf cancelConfigPressed:nil];
//        UIAlertView *findDeviceAlert = [[UIAlertView alloc] initWithTitle:@"发现新设备"
//                                                                  message:nil
//                                                                 delegate:weakSelf
//                                                        cancelButtonTitle:@"添加"
//                                                        otherButtonTitles:nil, nil];
//        [findDeviceAlert setTag:FIND_DEVICE_ALERT];
//        [findDeviceAlert show];
//    }];
//    
//}
//
//- (IBAction)cancelConfigPressed:(id)sender
//{
//    [self.aqWifiConfig Stop];
//    [self.lanFindServer Stop];
//    
//    [_cancelConfigButton setHidden:YES];
//    [_startConfigButton setHidden:NO];
//    
//    [_timer invalidate];
//    [_progressView setProgress:0];
//    _timerCount = 0;
//}
//
//
//
//- (void)timerUpdate:(NSTimer *)timer
//{
//    _timerCount++;
//    float progress = _timerCount / (float)CONFIG_TIME;
//    [_progressView setProgress: progress animated:YES];
//    
//    if (_timerCount >= CONFIG_TIME)
//    {
//        [_timer invalidate];
//        _timerCount = 0;
//        [self cancelConfigPressed:nil];
//        [[[UIAlertView alloc] initWithTitle:@"配置超时"
//                                    message:@"请尝试重新开始配置"
//                                   delegate:nil
//                          cancelButtonTitle:@"确定"
//                          otherButtonTitles:nil, nil] show];
//    }
//    NSLog(@"timerCount = %ld", (long)_timerCount);
//}
//
//#pragma -mark- Setter and Getter
//- (AQWifiConfig *)aqWifiConfig
//{
//    if (nil == _aqWifiConfig)
//    {
//        _aqWifiConfig = [[AQWifiConfig alloc] init];
//    }
//    return _aqWifiConfig;
//}
//
//- (LANFindServer *)lanFindServer
//{
//    if (nil == _lanFindServer)
//    {
//        _lanFindServer = [[LANFindServer alloc] init];
//    }
//    return _lanFindServer;
//}
//
//- (id)fetchSSIDInfo
//{
//    NSArray *ifs = (__bridge_transfer NSArray *)CNCopySupportedInterfaces();
//    NSDictionary *info;
//    for (NSString *ifnam in ifs)
//    {
//        info = (__bridge_transfer NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
//        if (info && [info count]) { break; }
//    }
//    return info;
//}
//
//#pragma -mark- UIAlertViewDelegate
//-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//    if (0 == buttonIndex)
//    {
//        if (alertView.tag == FIND_DEVICE_ALERT)
//        {
//            [self.navigationController popViewControllerAnimated:YES];
//        }
//        else if(alertView.tag == NO_WIFI_ALERT)
//        {
//            BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL);
//            if (canOpenSettings) {
//                NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
//                [[UIApplication sharedApplication] openURL:url];
//            }
//            [self.navigationController popViewControllerAnimated:YES];
//        }
//    }
//}
//
//#pragma -mark- UITextFieldDelegate
//- (BOOL)textFieldShouldReturn:(UITextField *)textField
//{
//    if (_ssidTextField == textField)
//    {
//        [_passwordTextField becomeFirstResponder];
//        return NO;
//    }
//    else if (_passwordTextField == textField)
//    {
//        [_passwordTextField resignFirstResponder];
//        return NO;
//    }
//    return YES;
//}
//
//@end
